package com.homepage.service;

import com.homepage.domain.EmployeePortrait;
import com.homepage.repository.EmployeePortraitRepository;
import com.homepage.service.dto.EmployeePortraitDTO;
import com.homepage.service.mapper.EmployeePortraitMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link EmployeePortrait}.
 */
@Service
@Transactional
public class EmployeePortraitService {

    private final Logger log = LoggerFactory.getLogger(EmployeePortraitService.class);

    private final EmployeePortraitRepository employeePortraitRepository;

    private final EmployeePortraitMapper employeePortraitMapper;

    public EmployeePortraitService(EmployeePortraitRepository employeePortraitRepository, EmployeePortraitMapper employeePortraitMapper) {
        this.employeePortraitRepository = employeePortraitRepository;
        this.employeePortraitMapper = employeePortraitMapper;
    }


    /**
     * Save a employeePortrait.
     *
     * @param employeePortraitDTO the entity to save.
     * @return the persisted entity.
     */
    public EmployeePortraitDTO save(EmployeePortraitDTO employeePortraitDTO) {
        log.debug("Request to save EmployeePortrait : {}", employeePortraitDTO);
        EmployeePortrait employeePortrait = employeePortraitMapper.toEntity(employeePortraitDTO);
        employeePortrait = employeePortraitRepository.save(employeePortrait);
        return employeePortraitMapper.toDto(employeePortrait);
    }

    /**
     * Partially update a employeePortrait.
     *
     * @param employeePortraitDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<EmployeePortraitDTO> partialUpdate(EmployeePortraitDTO employeePortraitDTO) {
        log.debug("Request to partially update EmployeePortrait : {}", employeePortraitDTO);

        return employeePortraitRepository
            .findById(employeePortraitDTO.getId())
            .map(existingEmployeePortrait -> {
                employeePortraitMapper.partialUpdate(existingEmployeePortrait, employeePortraitDTO);

                return existingEmployeePortrait;
            })
            .map(employeePortraitRepository::save)
            .map(employeePortraitMapper::toDto);
    }

    /**
     * Get all the employeePortraits.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<EmployeePortraitDTO> findAll() {
        log.debug("Request to get all EmployeePortraits");
        return employeePortraitRepository
            .findAll()
            .stream()
            .map(employeePortraitMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one employeePortrait by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<EmployeePortraitDTO> findOne(Long id) {
        log.debug("Request to get EmployeePortrait : {}", id);
        return employeePortraitRepository.findById(id).map(employeePortraitMapper::toDto);
    }

    @Transactional(readOnly = true)
    public Optional<EmployeePortraitDTO> findOneByEmployeeId(Long employeeId) {
        log.debug("Request to get EmployeePortrait : {}", employeeId);
        return employeePortraitRepository.findEmployeePortraitByEmployeeId(employeeId).map(employeePortraitMapper::toDto);
    }
    /**
     * Delete the employeePortrait by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete EmployeePortrait : {}", id);
        employeePortraitRepository.deleteById(id);
    }
}
